<template>
	<view class="page-my-card-code">
		<uni-qrcode ref="uqrcode" cid="course" :text="checkUrl" />
		<view class="note">
			<text>二维码有效期为五分钟，如果二维码已使用或者超过五分钟未使用，二维码将失效。若需重新获取二维码，请刷新。</text>
		</view>
	</view>
</template>

<script>
	import UniQrcode from '@/components/common/uni-qrcode'
	import * as membercardApi from '@/api/membercard.js'

	export default {
		components: {
			UniQrcode
		},
		data() {
			return {
				orderId: 0,
				codeToken: 0
			};
		},
		computed: {
			checkUrl() {
				let { protocol, host, pathname } = window.location
				return `${protocol}//${host}${pathname}/#/pages/mine/check-card?orderId=${this.orderId}&token=${this.codeToken}`
			}
		},
		onLoad(options) {
			this.orderId = options.orderId
			// 为了避免暴露用户token，重新生成一个二维码的token，该token有效期为5分钟，用完则删除
			this.getCodeToken()
		},
		methods: {
			getCodeToken() {
				membercardApi.getCodeToken().then(res => {
					this.codeToken = res.data.codeToken
					this.$nextTick(() => {
						this.$refs.uqrcode.make()
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.page-my-card-code{
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-top: 156rpx;
	.note{
		padding: 26rpx 0rpx;
		width: 640rpx;
		margin: 0 auto;
		text-align: center;
		line-height: 1.8;
		&>text{
			padding: 0 30rpx;
		}
	}
}
</style>
